/*
 * Created on Mar 18, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.engine;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

import edu.mit.simile.longwell.Longwell;
import edu.mit.simile.longwell.LongwellServer;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.query.bucket.DateTimeBucketer;
import edu.mit.simile.longwell.query.bucket.DistinctValueBucketer;
import edu.mit.simile.longwell.query.bucket.IntegerBucketer;
import edu.mit.simile.longwell.query.project.DateTimeProjector;
import edu.mit.simile.longwell.query.project.IntegerProjector;
import edu.mit.simile.longwell.query.project.PropertyProjector;
import edu.mit.simile.longwell.schema.IStructuredProperty;

/**
 * @author dfhuynh
 */
public abstract class EngineUtilities {
	public static class FacetInfo {
		public String	m_projectorName;
		public String	m_projectorParameter;
		public String	m_label;
		public String	m_bucketerName;
	}
	
	static public FacetInfo getFacetInfo(String propertyURI, boolean forward, String locale, Profile profile) {
		return getFacetInfo(
			profile.getSchemaModel().getStructuredProperty(
				profile.getModel().createProperty(propertyURI)),
			forward,
			locale,
			profile);
	}
	static public FacetInfo getFacetInfo(IStructuredProperty sProperty, boolean forward, String locale, Profile profile) {
		FacetInfo info = new FacetInfo();
		
		String propertyURI = sProperty.getURI();
		
		info.m_label = sProperty.getLabel(locale);
		info.m_projectorParameter = forward ? propertyURI : "!" + propertyURI;
		
		Model model = profile.getModel();
		Model defaultModel = 
			LongwellServer.getServer().getDefaultProfile().getModel();
		
		Property propertyTypePredicate = model.createProperty(Longwell.s_namespace + "propertyType");
		Property projectorPredicate = model.createProperty(Longwell.s_namespace + "projector");
		Property bucketerPredicate = model.createProperty(Longwell.s_namespace + "bucketer");
		
		/*
		 * Get the Longwell property type
		 */
		Resource property = model.createResource(propertyURI);
		Resource propertyType = null;
		try {
			propertyType = (Resource) model.getProperty(
					property, propertyTypePredicate).getObject();
		} catch (Exception e) {
		}
		if (propertyType == null) {
			try {
				propertyType = (Resource) defaultModel.getProperty(
							property, propertyTypePredicate).getObject();
			} catch (Exception e) {
			}
		}
		
		/*
		 * Get the projector and bucketer for that property type
		 */
		if (propertyType != null) {
			try {
				info.m_projectorName = model.getProperty(
					propertyType, projectorPredicate).getString();
				info.m_bucketerName = model.getProperty(
					propertyType, bucketerPredicate).getString();
			} catch (Exception e) {
			}
			
			if (info.m_projectorName == null || info.m_bucketerName == null) {
				try {
					info.m_projectorName = defaultModel.getProperty(
						propertyType, projectorPredicate).getString();
					info.m_bucketerName = defaultModel.getProperty(
						propertyType, bucketerPredicate).getString();
				} catch (Exception e) {
				}
			}
		}
		
		/*
		 * If no property type specified, suggest default projectors and bucketers
		 */
		if (info.m_projectorName == null || info.m_bucketerName == null) {
			if (sProperty.getTypeConfidence(IStructuredProperty.s_type_integer) > 0.5) {
				info.m_projectorName = IntegerProjector.class.getName();
				info.m_bucketerName = IntegerBucketer.class.getName();
			} else if (sProperty.getTypeConfidence(IStructuredProperty.s_type_dateTime) > 0.5) {
				info.m_projectorName = DateTimeProjector.class.getName();
				info.m_bucketerName = DateTimeBucketer.class.getName();
			} else {
				info.m_projectorName = PropertyProjector.class.getName();
				info.m_bucketerName = DistinctValueBucketer.class.getName();
			}
		}
		
		return info;
	}
	

}
